Celeb_a(self making)

import tensorflow as tf
import pathlib
import tensorflow_datasets as tfds
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
imgdir_path=pathlib.Path('C:/Users/csian/tensorflow_datasets/downloads/manual/img_align_celeba')
file_list=sorted([str(path) for path in imgdir_path.glob('*.jpg')])
fp=open('C:/Users/csian/tensorflow_datasets/downloads/manual/list_attr_celeba.txt', 'r')
line=fp.readline()
leng=int(line)
line=fp.readline()
index=line.split(' ')
loc=index.index('Male')
sex=[]
for i in range(leng):
line=fp.readline()
line=line[11:-1]
line=line.replace(' ', ' ')
line=line.split(' ')
sex.append(int(line[loc]))
fp.close()
fig=plt.figure(figsize=(12, 8))
for i in range(18):
ax=fig.add_subplot(3, 6, i+1)
ax.set_xticks([]); ax.set_yticks([])
image=Image.open(file_list[i])
data=np.array(image)
ax.imshow(data)
ax.set_title('{}'.format(sex[i]), size=15)
plt.tight_layout()
plt.show()
데이터셋 분류는 아직 안함
import tensorflow as tf
import pathlib
import tensorflow_datasets as tfds
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from sklearn.model_selection import train_test_split
imgdir_path=pathlib.Path('C:/Users/csian/tensorflow_datasets/downloads/manual/img_align_celeba')
file_list=sorted([str(path) for path in imgdir_path.glob('*.jpg')])
fp=open('C:/Users/csian/tensorflow_datasets/downloads/manual/list_attr_celeba.txt', 'r')
line=fp.readline()
leng=int(line)
line=fp.readline()
index=line.split(' ')
loc=index.index('Male')
sex=[]
for i in range(leng):
line=fp.readline()
line=line[11:-1]
line=line.replace(' ', ' ')
line=line.split(' ')
if line[loc]=='-1':
li=0
else:
li=1
sex.append(li)
fp.close()
labels=tf.convert_to_tensor(sex)
tensor_list=[]
for i in range(len(sex)):
image=Image.open(file_list[i])
data=np.array(image)
td=tf.convert_to_tensor(data)
tensor_list.append(td)
print('%d / %d' %(i+1, len(sex)))
ds_x=tf.data.Dataset.from_tensor_slices(tensor_list)
ds_y=tf.data.Dataset.from_tensor_slices(labels)
ds_joint=tf.data.Dataset.zip((ds_x, ds_y))
image, label=next(iter(ds_joint))
print(image.shape, label)
(202599, 218, 178, 3) tensor
36개의 데이터만을 이용해서 데이터셋 만들기
import tensorflow as tf
import pathlib
import tensorflow_datasets as tfds
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from sklearn.model_selection import train_test_split
imgdir_path=pathlib.Path('C:/Users/csian/tensorflow_datasets/downloads/manual/img_align_celeba')
file_list=sorted([str(path) for path in imgdir_path.glob('*.jpg')])
fp=open('C:/Users/csian/tensorflow_datasets/downloads/manual/list_attr_celeba.txt', 'r')
line=fp.readline()
leng=int(line)
line=fp.readline()
index=line.split(' ')
loc=index.index('Male')
sex=[]
for i in range(leng):
line=fp.readline()
line=line[11:-1]
line=line.replace(' ', ' ')
line=line.split(' ')
if line[loc]=='-1':
li=0
else:
li=1
sex.append(li)
fp.close()
labels=tf.convert_to_tensor(sex)
tensor_list=[]
for i in range(30):
image=Image.open(file_list[i])
data=np.array(image)
td=tf.convert_to_tensor(data)
tensor_list.append(td)
print('%d / %d' %(i+1, len(sex)))
ds_test=tf.data.Dataset.from_tensor_slices(tensor_list[:15])
ds_test_y=tf.data.Dataset.from_tensor_slices(labels[:15])
ds_train=tf.data.Dataset.from_tensor_slices(tensor_list[15:])
ds_train_y=tf.data.Dataset.from_tensor_slices(labels[15:])
ds_train_joint=tf.data.Dataset.zip((ds_train, ds_train_y))
ds_test_joint=tf.data.Dataset.zip((ds_test, ds_test_y))
ds_train_batch=ds_train_joint.batch(15)
fig=plt.figure(figsize=(10, 8))
images, labels=next(iter(ds_train_batch))
for i, (image, label) in enumerate(zip(images, labels)):
ax=fig.add_subplot(3, 5, i+1)
ax.set_xticks([]);ax.set_yticks([])
ax.imshow(image)
ax.set_title('{}'.format(label), size=15)
plt.show()